package com.example.hosptialtag.controller;

import com.example.hosptialtag.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.concurrent.TimeUnit;

/**
 * @Author liuyuhao&caiang
 * @Date 2022/2/21
 */

@Controller
@RequestMapping(value="/redis")
public class RedisController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RedisService redisService;

    @GetMapping("/put")
    public String put(@RequestParam(required = true) String key, @RequestParam(required = true) String value){
        // 设置数据 20秒过期
        redisTemplate.opsForValue().set(key,value,20, TimeUnit.SECONDS);
        return "SUC";
    }

    @GetMapping("/get")
    public String get(@RequestParam(required = true) String key){
        return (String)redisTemplate.opsForValue().get(key);
    }

    @GetMapping("/putSet")
    public String putSet(@RequestParam(required = true) String key, @RequestParam(required = true) String value) {
        return redisService.putSet(key,value);
    }

    @GetMapping("/getSet")
    public String getSet(@RequestParam(required = true) String key){
        return redisService.getSet(key).toString();
    }

}

